	package com.app.controller.security;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.app.service.security.LoginService;
import com.app.service.webContent.WebContent;
import com.app.to.Menu;
import com.app.to.Usuario;

@RequestMapping("/security")
@Controller
public class SecurityController {
	@Autowired
	private LoginService service;

	@Autowired
	private WebContent webContent;

	
	private static final Logger logger = LoggerFactory
			.getLogger(SecurityController.class);

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		return new ModelAndView("security/login", "command", new Usuario());
	}

	@RequestMapping(value = "/validateLogin", method = RequestMethod.POST)
	public ModelAndView validateLogin(
			@ModelAttribute("usuario") Usuario usuario, Model model) {//ModelMap
		logger.info("Welcome home! The client locale is {}.");

		usuario = service.validateLoginAccess(usuario);

		if (usuario != null) {
			List<Menu> option = webContent.getListMenuOption();
			/*List<Marca> marcas = marcaService.getListMarca();
			if(marcas!=null && marcas.size() > 0){
				for (Marca m : marcas) {
					System.out.println(m.getDescripcion());
					System.out.println(m.getId());
					System.out.println(m.getEstado());
				}
			}*/
			model.addAttribute("user", usuario.getUser());
			model.addAttribute("nivelAcceso", usuario.getNivelAcceso());
			model.addAttribute("listOption", option);
			//return new ModelAndView("security/successLogin");
			return new ModelAndView("admin/welcome");
		}
		model.addAttribute("mensaje", "Error al ingresar");

		return new ModelAndView("security/login", "command", new Usuario());

	}

	/*
	 * @RequestMapping(value = "/validateLogin", method = RequestMethod.POST)
	 * public String validateLogin(@ModelAttribute("usuario") Usuario usuario,
	 * ModelMap model) { logger.info("Welcome home! The client locale is {}.");
	 * //LoginService service = SecurityDelegate.getLoginService(); usuario =
	 * service.validateLoginAccess(usuario);
	 * 
	 * if(usuario != null) { model.addAttribute("user", usuario.getUser());
	 * model.addAttribute("nivelAcceso", usuario.getNivelAcceso()); return
	 * "security/successLogin"; }
	 * model.addAttribute("mensaje","Error al ingresar"); return
	 * "security/wrongLogin";
	 * 
	 * }
	 */

}
